package com.webware.view.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.webware.view.client.mvp.AppPlaceHistoryMapper;
import com.webware.view.client.mvp.BodyActivityMapper;
import com.webware.view.client.mvp.HeaderActivityMapper;
import com.webware.view.client.place.StartPlace;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Index implements EntryPoint {
    private final SimpleLayoutPanel header = new SimpleLayoutPanel();
    private final SimpleLayoutPanel body = new SimpleLayoutPanel();
    private final Place defaultPlace = new StartPlace();

    @SuppressWarnings("unused")
    private final Messages messages = GWT.create(Messages.class);

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
	initDisplay();
	initMvp();
    }

    private void initDisplay() {
	DockLayoutPanel panel = new DockLayoutPanel(Unit.PX);
	panel.addNorth(header, 70);
	panel.add(body);
	RootLayoutPanel.get().add(panel);
    }

    private void initMvp() {
	ClientFactory clientFactory = GWT.create(ClientFactoryImpl.class);
	EventBus eventBus = clientFactory.getEventBus();
	PlaceController placeController = clientFactory.getPlaceController();

	// Haader
	ActivityMapper activityMapper = new HeaderActivityMapper(clientFactory);
	ActivityManager activityManager = new ActivityManager(activityMapper,
		eventBus);
	activityManager.setDisplay(header);

	// Body
	activityMapper = new BodyActivityMapper(clientFactory);
	activityManager = new ActivityManager(activityMapper, eventBus);
	activityManager.setDisplay(body);

	// Start PlaceHistoryHandler with our PlaceHistoryMapper
	AppPlaceHistoryMapper historyMapper = GWT
		.create(AppPlaceHistoryMapper.class);
	PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
		historyMapper);
	historyHandler.register(placeController, eventBus, defaultPlace);

	AppController controller = new AppController(clientFactory);
	
	historyHandler.handleCurrentHistory();

    }
}
